package com.xh.chat.netty.event;

import com.xh.chat.netty.annotation.EventProcess;
import com.xh.chat.netty.dto.DataContentDTO;
import com.xh.chat.netty.enums.MsgActionEnum;
import com.xh.chat.service.UserService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wen
 * @apiNote ConnectEventHandler
 * @since 2021-08-09
 */
@Slf4j
@Service
@EventProcess(MsgActionEnum.SIGNED)
public class SignedEventHandler implements EventHandler {

    @Autowired
    private UserService userService;

    @Override
    public void handle(DataContentDTO dataContentDTO, Channel currentChannel) {
        //  2.3  签收消息类型，针对具体的消息进行签收，修改数据库中对应消息的签收状态[已签收]
        // 扩展字段在signed类型的消息中，代表需要去签收的消息id，逗号间隔
        String msgIdsStr = dataContentDTO.getExtend();
        String[] msgIds = msgIdsStr.split(",");
        List<String> msgIdList = Arrays.asList(msgIds);

        log.info(msgIdList.toString());

        if (!msgIdList.isEmpty()) {
            List<Long> idList = msgIdList.stream()
                    .map(Long::valueOf).distinct().collect(Collectors.toList());
            // 批量签收
            userService.updateMsgSigned(idList);
        }
    }

}
